//
//  PlayVRViewController.m
//  VRDemo
//
//  Created by Agensn on 2017/3/9.
//  Copyright © 2017年 🌹Agensn_Master🌹. All rights reserved.
//

#import "PlayVRViewController.h"
#import <UtoVRPlayer/UtoVRPlayer.h>
#import "AppDelegate.h"

@interface PlayVRViewController ()<UVPlayerDelegate>

@end

@implementation PlayVRViewController

#pragma mark 强制横屏
-(void)viewWillAppear:(BOOL)animated{
    //在视图出现的时候，将allowRotate改为1，
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;
    
    self.navigationItem.hidesBackButton = YES;
}
 //一开始的方向  很重要
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;
}

/*
 可能会有些不适
 */
-(UVPlayer *)player {
    if (_player == nil) {
        _player = [[UVPlayer alloc] initWithConfiguration:nil];
        _player.delegate = self;
    }
    return _player;
}
-(NSMutableArray *)itemsToPlay {
    if (_itemsToPlay == nil) {
        _itemsToPlay = [[NSMutableArray alloc]init];
    }
    return _itemsToPlay;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //将播放视图添加到当前界面
    [self.view addSubview:self.player.playerView];
    
    if (self.player.viewStyle == UVPlayerViewStyleDefault) {
//        * 可以设置横屏返回按钮的回调事件。
        [self.player setHorizontalBackButtonTarget:self selector:@selector(back:)];
    }
    
    //把要播放的内容添加到播放器
    UVPlayerItem *item = [[UVPlayerItem alloc]initWithURL:[NSURL URLWithString:self.Path2K]];
    [self.player appendItem:item];
}
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //调整frame。你可以使用任何其它布局方式保证播放视图是你期望的大小
    CGRect frame;
    if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
        frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44);
    } else {
        frame = CGRectMake(0, 64 + 44, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 44 -44);
    }
    //位置
    self.player.playerView.frame = self.view.frame;
    
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //退出时不要忘记调用prepareToRelease
    [self.player prepareToRelease];
    
}


-(void)back:(UIButton*)sender {
    //创建动画
    CATransition *animation = [CATransition animation];
    //设置运动轨迹的速度
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    //设置动画类型为立方体动画
    animation.type = @"CameraIrisHollowClose";
    //设置动画时长
    animation.duration =0.5f;
    //设置运动的方向
    animation.subtype =kCATransitionFromRight;
    //控制器间跳转动画
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

-(void)player:(UVPlayer *)player willBeginPlayItem:(UVPlayerItem *)item {
    if (player.viewStyle == UVPlayerViewStyleDefault) {
        //设置横屏显示的title为当前播放资源的路径。你可以设置为其它的任何内容
        [player setTitleText:item.path];
    }
}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
